package com.next

class Lead
{
    String fullName
    Salutation salutation
    //称谓
    String title
    //头衔
    String email
    String cellphone
    String phone

    String company
    Industry industry
    Integer numberOfEmployees = 0
    String website
    String address

    LeadRating rating
    LeadStatus status

    User createBy
    User modifyBy
    User user

    String description

    Boolean active = true
    Account account

    Date createdDate = new Date()
    Date modifiedDate = new Date()

    static hasMany = [addresses: Address, users: LeadTeam]

    static constraints = {
        address maxSize: 128, nullable: true, blank: true
        fullName nullable: true, blank: true
        salutation nullable: true, blank: true
        title nullable: true, blank: true
        email nullable: true, blank: true
        cellphone nullable: true, blank: true
        phone nullable: true, blank: true
        company maxSize: 128, nullable: false, blank: false
        industry nullable: true, blank: true
        website nullable: true, blank: true
        numberOfEmployees nullable: true, blank: true
        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true

        rating nullable: true, blank: true
        status nullable: true, blank: true

        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true
        user nullable: true, blank: true

        active nullable: true, blank: true
        account nullable: true, blank: true

        description nullable: true, blank: true, type: "text"
    }

    String toString()
    {
        company
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
        if (!user)
        {
            user = createBy
        }

        // 初始化 所有者 线索权限
        LeadTeam leadTeam = new LeadTeam(lead: this, user: user, teamRole: TeamRole.findByName('Edit'))
        this.addToUsers(leadTeam)

        // 初始化 上级用户 线索权限
        if (user.shareLeadToSupervisor)
        {
            User reportTo = user.reportTo
            while (reportTo)
            {
                LeadTeam leadTeam1 = new LeadTeam(lead: this, user: reportTo, teamRole: TeamRole.findByName('Read'))
                this.addToUsers(leadTeam1)

                reportTo = reportTo.reportTo
            }
        }
    }

    def afterInsert()
    {
        def flexFieldCategories = FlexFieldCategory.findAllByObjectType('Lead')
        flexFieldCategories?.each {
            if (it?.condition)
            {
                def result = false
                try
                {
                    def shell = new GroovyShell()
                    def closure = shell.evaluate(it?.condition?.script)
                    result = closure(this)
                }
                catch (Exception e)
                {
                    log.error e
                }
                if (result)
                {
                    initFlexField(it)
                }
            }
            else
            {
                initFlexField(it)
            }
        }

    }

    def initFlexField(FlexFieldCategory flexFieldCategory)
    {
        def fields = FlexField.findAllByCategory(flexFieldCategory)
        fields?.each {
            def field = new FlexFieldInstance(category: flexFieldCategory, field: it, lead: this)
            field.save()
        }
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }
}
